package com.home.cca.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.home.cca.R;
import com.home.cca.model.Goods;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsViewHolder> {

    private final Context context;
    private final List<Goods> goodsList = new ArrayList<>();
    private Consumer<String> toast;

    public GoodsAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setGoodsList(List<Goods> goodsList) {
        this.goodsList.clear();
        this.goodsList.addAll(goodsList);
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false);
        return new GoodsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsViewHolder holder, int position) {
        Goods goods = goodsList.get(position);
        // 绑定商品对象
        holder.bind(goods, toast);
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    public void setToast(Consumer<String> toast) {
        this.toast = toast;
    }
}